# @Time : 2019/12/17 15:37 
# @Author : Fioman 
# @Phone : 13149920693

from socket import *
import threading
from settings import *
address='127.0.0.1'     #监听哪些网络  127.0.0.1是监听本机 0.0.0.0是监听整个网络
port=5000             #监听自己的哪个端口
s = socket(AF_INET, SOCK_STREAM)
s.bind((address,port))
s.listen(2)     #最大连接数

def tcplink(sock,addr):
    while True:
        recvdata=clientsock.recv(BUFFER_SIZE)
        if recvdata=='exit' or not recvdata:
            break
        with open(r'./server_send.xml','rb') as f:
            send_data = f.read()
            send_data = b'Char(2)' + send_data + b'Char(3)'
        print(recvdata)
        with open(r'./server_recv.xml','wb') as f:
            f.write(recvdata[7:-7])
        clientsock.send(send_data)
    clientsock.close()

while True:
    try:
        clientsock,clientaddress=s.accept()
        print('connect from:',clientaddress)
        #传输数据都利用clientsock，和s无关
        t=threading.Thread(target=tcplink,args=(clientsock,clientaddress))  #t为新创建的线程
        t.start()
    except Exception as e:
        print(e)
        break
s.close()
